/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Iterator;
import java.util.List;

import javax.management.Query;

import Util.HibernetUtil;

import model.Account;
import model.Module;
import model.Thematic;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author mehdi
 */
public class Dao implements Idao {
    

    @Override
    public void addcompte(Account account) {
        Session session = HibernetUtil.getSessionFactory().openSession();
	session.beginTransaction();
             session.save(account);
		session.getTransaction().commit();
    }

    @Override
    public void updatecompte(Account account) {
        Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
                session.update(account);

		session.getTransaction().commit();
		session.close();
       
    }

    @Override
    public void deletecompte(Long pk) {
         Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
                Account et = (Account) session.load(Account.class, pk);
		session.delete(et);
		session.beginTransaction().commit();
		session.close();

      
    }

    @Override
    public Account findcompte(Long pk) {
        Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Account q = (Account) session.get(Account.class,pk);

		Account count = q;

		return count;
    }

    @Override
    public void addtheme(Thematic theme) {
      Session session = HibernetUtil.getSessionFactory().openSession();
	session.beginTransaction();
             session.save(theme);
		session.getTransaction().commit();
    }

    @Override
    public void updatetheme(Thematic theme) {
        Session session = HibernetUtil.getSessionFactory().openSession();
	session.beginTransaction();
             session.update(theme);
		session.getTransaction().commit();
    }

    @Override
    public Thematic findtheme(Long pk) {
       Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		Thematic q = (Thematic) session.get(Thematic.class,pk);

		Thematic theme = q;

		return theme;
    }

    @Override
    public void deletetheme(Long pk) {
          Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
                Thematic et = (Thematic) session.load(Thematic.class, pk);
		session.delete(et);
		session.beginTransaction().commit();
		session.close();
    }

	@Override
	public List<Account> findcompteAll() {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<Account> q = session.createQuery("from Account").list();
		session.beginTransaction().commit();

		return q;

			

	
	}

	@Override
	public List<Thematic> findthemeAll() {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<Thematic> q = session.createQuery("from Thematic").list();
		session.beginTransaction().commit();

		return q;

	}

	@Override
	public Account findcompte(String Login) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
	List<Account> q=   session.createCriteria(Account.class).add(Restrictions.like("Login",Login)).list();
	
Account aa=new Account();
		for(Account ac:q){
			System.out.println(ac.getLogin());
			aa=ac;
		} 
	
	
		//q.setParameter("login",login);
		//Account ac=(Account)q. ;
		
		session.beginTransaction().commit();

	return aa;
	}

	@Override
	public Thematic findtheme(String titre) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
	List<Thematic> q=   session.createCriteria(Thematic.class).add(Restrictions.like("Hoti",titre)).list();
	
Thematic aa=new Thematic();
		for(Thematic ac:q){
			System.out.println(ac.getHoti());
			aa=ac;
		} 
	
		
		session.beginTransaction().commit();

		return aa;
	}

	@Override
	public void addmodule(Module module) {
		 Session session = HibernetUtil.getSessionFactory().openSession();
			session.beginTransaction();
		             session.save(module);
				session.getTransaction().commit();
		    
		
	}

	@Override
	public void updatemodule(Module module) {
		   Session session = HibernetUtil.getSessionFactory().openSession();
			session.beginTransaction();
		             session.update(module);
				session.getTransaction().commit();
		    
		
	}

	@Override
	public Module findmodule(Long pk) {
		 Session session = HibernetUtil.getSessionFactory().openSession();
			session.beginTransaction();
			Module q = (Module) session.get(Module.class,pk);

			Module theme = q;

			return theme;


	}

	@Override
	public void deletemodule(Long pk) {
		  Session session = HibernetUtil.getSessionFactory().openSession();
			session.beginTransaction();
	                Module et = (Module) session.load(Module.class, pk);
			session.delete(et);
			session.beginTransaction().commit();
			session.close();
		
	}

	@Override
	public List<Module> findmoduleAll() {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
		List<Module> q = session.createQuery("from Module").list();
		session.beginTransaction().commit();

		return null;
	}

	@Override
	public Module findmodule(String titre) {
		Session session = HibernetUtil.getSessionFactory().openSession();
		session.beginTransaction();
	List<Module> q=   session.createCriteria(Module.class).add(Restrictions.like("Homo",titre)).list();
	
Module aa=new Module();
		for(Module ac:q){
			System.out.println(ac.getHomo());
			aa=ac;
		} 	
		session.beginTransaction().commit();
		return aa;
	}

	@Override
	public void affecterModulTheme(Long num, Long code) {
		
		 Session session= HibernetUtil.getSessionFactory().openSession();
		  session.beginTransaction(); 
		  Module form=(Module)session.load(Module.class,num); 
		  Thematic et=(Thematic)session.load(Thematic.class,code); 
		  et.getModule().add(form);
		  form.setThem(et);
		  
		  session.beginTransaction().commit(); session.close();
		  
		
	}

}
